package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ContextDao;
import com.rstrulak.dao.UserDao;
import com.rstrulak.model.Context;
import com.rstrulak.utils.JSFUtils;

@Component("newContextBean")
@Scope("request")
public class NewContextBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	private ContextDao contextDao;
	
	@Autowired
	UserDao userDao;

	private Context context;
	
	private String contextName;
	
	private String contextDescription;

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public String getContextName() {
		return contextName;
	}

	public void setContextName(String contextName) {
		this.contextName = contextName;
	}

	public String getContextDescription() {
		return contextDescription;
	}

	public void setContextDescription(String contextDescription) {
		this.contextDescription = contextDescription;
	}

	public void saveContext() throws ServletException, IOException {

		context = new Context();
		context.setDescription(contextDescription);
		context.setName(contextName);
		contextDao.save(context);
		JSFUtils.redirect("/protected/context.xhtml");
	}

	public void cancelAddingContext() throws ServletException, IOException {
		JSFUtils.redirect("/protected/context.xhtml");
	}

}
